package game.entity;

import game.util.Direction;
import game.util.GameConstant;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;

import java.util.*;

/**
 * @author zsq
 * @date 2023/1/15 下午8:59
 */
public class Enemy extends AbstractEntity {

    private Map<Direction, List<Image>> imageMap;

    private int imageIndex;

    public Enemy(int x, int y, char c) {
        super(x, y, c);
        broken = true;
        imageMap = new HashMap<>();
        direction = Direction.DOWN;
        List<Image> downImages = new ArrayList<>();
        List<Image> leftmages = new ArrayList<>();
        List<Image> rightImages = new ArrayList<>();
        List<Image> upImages = new ArrayList<>();

        if (c == 'R') {
            downImages.add(new Image("file:src/game/image/red_down1.png"));
            downImages.add(new Image("file:src/game/image/red_down2.png"));
            downImages.add(new Image("file:src/game/image/red_down3.png"));
            downImages.add(new Image("file:src/game/image/red_down4.png"));
            imageMap.put(Direction.DOWN, downImages);
            leftmages.add(new Image("file:src/game/image/red_left1.png"));
            leftmages.add(new Image("file:src/game/image/red_left2.png"));
            leftmages.add(new Image("file:src/game/image/red_left3.png"));
            leftmages.add(new Image("file:src/game/image/red_left4.png"));
            imageMap.put(Direction.LEFT, leftmages);
            rightImages.add(new Image("file:src/game/image/red_right1.png"));
            rightImages.add(new Image("file:src/game/image/red_right2.png"));
            rightImages.add(new Image("file:src/game/image/red_right3.png"));
            rightImages.add(new Image("file:src/game/image/red_right4.png"));
            imageMap.put(Direction.RIGHT, rightImages);
            upImages.add(new Image("file:src/game/image/red_up1.png"));
            upImages.add(new Image("file:src/game/image/red_up2.png"));
            upImages.add(new Image("file:src/game/image/red_up3.png"));
            upImages.add(new Image("file:src/game/image/red_up4.png"));
            imageMap.put(Direction.UP, upImages);
        } else {
            downImages.add(new Image("file:src/game/image/yellow_down1.png"));
            downImages.add(new Image("file:src/game/image/yellow_down2.png"));
            downImages.add(new Image("file:src/game/image/yellow_down3.png"));
            downImages.add(new Image("file:src/game/image/yellow_down4.png"));
            imageMap.put(Direction.DOWN, downImages);
            leftmages.add(new Image("file:src/game/image/yellow_left1.png"));
            leftmages.add(new Image("file:src/game/image/yellow_left2.png"));
            leftmages.add(new Image("file:src/game/image/yellow_left3.png"));
            leftmages.add(new Image("file:src/game/image/yellow_left4.png"));
            imageMap.put(Direction.LEFT, leftmages);
            rightImages.add(new Image("file:src/game/image/yellow_right1.png"));
            rightImages.add(new Image("file:src/game/image/yellow_right2.png"));
            rightImages.add(new Image("file:src/game/image/yellow_right3.png"));
            rightImages.add(new Image("file:src/game/image/yellow_right4.png"));
            imageMap.put(Direction.RIGHT, rightImages);
            upImages.add(new Image("file:src/game/image/yellow_up1.png"));
            upImages.add(new Image("file:src/game/image/yellow_up2.png"));
            upImages.add(new Image("file:src/game/image/yellow_up3.png"));
            upImages.add(new Image("file:src/game/image/yellow_up4.png"));
            imageMap.put(Direction.UP, upImages);
        }
    }


    public void move(){
        if (direction == Direction.UP) {
            y = y - GameConstant.PIXEL;
        } else if (direction == Direction.DOWN) {
            y = y + GameConstant.PIXEL;
        } else if (direction == Direction.LEFT) {
            x = x - GameConstant.PIXEL;
        } else {
            x = x + GameConstant.PIXEL;
        }
    }

    public void changeDirection(){
        if (c == 'R') {
            Direction[] values = Direction.values();
            List<Direction> directions = Arrays.asList(values);
            Collections.shuffle(directions);
            direction = directions.get(0);
        } else {
            if (direction == Direction.UP) {
                direction = Direction.RIGHT;
                return;
            }

            if (direction == Direction.RIGHT) {
                direction = Direction.DOWN;
                return;
            }

            if (direction == Direction.DOWN) {
                direction = Direction.LEFT;
                return;
            }
            if (direction == Direction.LEFT) {
                direction = Direction.UP;
            }
        }
    }

    @Override
    public void draw(GraphicsContext g, int frame) {
        int value = frame % 2;
        if (value == 0) {
            if (imageIndex < 3) {
                imageIndex++;
            } else {
                imageIndex = 0;
            }
        }
        List<Image> images = imageMap.get(direction);
        image = images.get(imageIndex);
        super.draw(g, frame);
    }
}